package com.guotd.jspwork2.controller;

import com.guotd.jspwork2.utils.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet("/setPicture")
@MultipartConfig
public class setPictureServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String id = request.getParameter("id");
        String kind = request.getParameter("kind");
        Part picture=request.getPart("picture");
        String path= "D:\\我的代码\\Web\\jspWork2\\src\\main\\webapp\\imgs\\";
        path+=picture.getSubmittedFileName();
        picture.write(path);
        String sql="update tv set picture=? where id=?";
        String []args={picture.getSubmittedFileName(),id};
        int row = JDBCUtil.update(sql, args);
        if(row>0){
            request.getSession().setAttribute("tv_msg", "上传图片成功");
            request.getSession().setAttribute("kind", kind);
            request.getRequestDispatcher("getAllTVs").forward(request, response);
        }
        else {
            request.getSession().setAttribute("tv_msg","上传图片失败");
            request.getSession().setAttribute("kind", kind);
            request.getRequestDispatcher("getAllTVs").forward(request, response);
        }
    }
}